using System.Collections.Generic;
using System.IO;
using System.Text;

namespace DatabaseUnitTesting.Utilities.StoredProcedure
{
    internal static class SqlFileAdapter
    {
        internal static void WriteCommandsToFile(List<string> commands, string filename)
        {
            TextWriter writer = new StreamWriter(filename, false);

            foreach(string command in commands)
                writer.WriteLine(command);

            writer.Close();
        }

        internal static List<string> ReadCommandsFromFile(string filename)
        {
            using(TextReader fileReader = new StreamReader(filename))
            {
                List<string> commands = new List<string>();
                StringBuilder command = new StringBuilder();
                string line;
                while((line = fileReader.ReadLine()) != null)
                    if(!line.StartsWith("--"))
                    {
                        command.Append("\n");
                        command.Append(line);
                    } else
                    {
                        if(command.Length > 0)
                            commands.Add(command.ToString());
                        command = new StringBuilder(line);
                    }
                if(command.Length > 0)
                    commands.Add(command.ToString());

                return commands;
            }
        }
    }
}